#!/usr/bin/env bash

requirements_openindiana_lib_installed()
{
  pkg info "$1" > /dev/null 2>&1 || return $?
}

requirements_openindiana_lib_available()
{
  pkg info -r "$1" > /dev/null 2>&1 || return $?
}

requirements_openindiana_custom_lib_installed()
{
  pkginfo -q "$1" || return $?
}

requirements_openindiana_libs_install()
{
  __rvm_try_sudo pkg install "$@" ||
  {
    \typeset ret=$?
    case $ret in
      (4) return 0 ;; # means the package does not need updates
    esac
    return $ret
  }
}

requirements_openindiana_update_system()
{
  __rvm_try_sudo pkg refresh && __rvm_try_sudo pkg update ||
  {
    \typeset ret=$?
    case $ret in
      (4) return 0 ;; # means nothing to install
    esac
    return $ret
  }
}

requirements_openindiana_check_custom()
{
  for lib in "$@"
  do
    [[ " ${packages_custom[*]} " == *" $lib "* ]] ||
    requirements_openindiana_custom_lib_installed "$lib" || __rvm_add_once packages_custom "$lib"
  done
  unset lib
}

requirements_openindiana_check_opencsw_enabled()
{
  requirements_openindiana_custom_lib_installed CSWpkgutil || return $?
}

requirements_openindiana_enable_opencsw()
{
  __rvm_try_sudo pkgadd -a $rvm_path/config/solaris/noask -d https://get.opencsw.org/now CSWpkgutil > /dev/null 2>&1 || return $?
}

requirements_openindiana_install_custom()
{
  requirements_openindiana_check_opencsw_enabled || requirements_openindiana_enable_opencsw
  __rvm_try_sudo /opt/csw/bin/pkgutil -iy "$@" -t https://mirror.opencsw.org/opencsw/unstable || return $?
}

requirements_openindiana_after()
{
  # https://www.illumos.org/issues/782
  if ! __rvm_which automake >/dev/null 2>&1 && __rvm_which /usr/bin/automake-1.10 >/dev/null 2>&1
  then ln -s /usr/bin/automake-1.10 "${rvm_bin_path}"/automake
  fi
  if ! __rvm_which aclocal >/dev/null 2>&1 && __rvm_which /usr/bin/aclocal-1.10 >/dev/null 2>&1
  then ln -s /usr/bin/aclocal-1.10 "${rvm_bin_path}"/aclocal
  fi
}

requirements_openindiana_define()
{
  case "$1" in
    (rvm)
      requirements_check bash curl text/gnu-patch
      ;;

    (jruby*)
      requirements_check make jdk

      if
        is_head_or_disable_binary "$1"
      then
        requirements_check git
        case $( jruby_installation_method  "$1" ) in
          ant) requirements_check apache-ant             ;;
          mvn) requirements_check_custom_after mvn=maven ;;
        esac
      fi
      ;;

    (ir*)
      requirements_check mono
      ;;

    (opal)
      requirements_check runtime/javascript/nodejs
      ;;

    (*-head)
      requirements_check git
      requirements_openindiana_define "${1%-head}"
      ;;

    (*)
      requirements_check_fallback \
        developer/gcc47 developer/gcc-47 \
        developer/gcc46 developer/gcc-46 \
        developer/gcc44 developer/gcc-43 developer/gcc/gcc-43
      requirements_check_fallback \
        developer/build/automake \
        developer/build/automake-111 \
        developer/build/automake-110
      requirements_check \
        text/gnu-patch system/header developer/build/autoconf \
        developer/build/libtool system/library/math/header-math \
        file/gnu-coreutils developer/object-file developer/build/gnu-make
      requirements_openindiana_check_custom CSWlibyaml-dev
      __rvm_update_configure_opt_dir "$1" "/opt/csw"
      ;;
  esac
}

requirements_openindiana_after()
{
  if is_a_function requirements_openindiana_after_${_system_name}
  then requirements_openindiana_after_${_system_name} || return $?
  else requirements_openindiana_after_Other || return $?
  fi
}

requirements_openindiana_after_OmniOS()
{
  case "${packages_installed[*]}" in
    (*developer/gcc47*)
      __rvm_update_configure_env CC="/opt/gcc-4.7.2/bin/gcc" ;;
    (*developer/gcc-3*)
      __rvm_update_configure_env CC="/usr/sfw/bin/gcc" ;;
    (*)
      return 1 ;;
  esac
}

requirements_openindiana_after_Other()
{
  case "${packages_installed[*]}" in
    (*developer/gcc47*|*developer/gcc-47*)
      __rvm_update_configure_env CC="/usr/gcc/4.7/bin/gcc" ;;
    (*developer/gcc46*|*developer/gcc-46*)
      __rvm_update_configure_env CC="/usr/gcc/4.6/bin/gcc" ;;
    (*developer/gcc44*)
      __rvm_update_configure_env CC="/usr/gcc/4.4/bin/gcc" ;;
    (*developer/gcc-43*|*developer/gcc/gcc-43*)
      __rvm_update_configure_env CC="/usr/gcc/4.3/bin/gcc" ;;
    (*)
      return 1 ;;
  esac
}

requirements_omnios_before()
{
  __lib_type=openindiana
}
